﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Ledict.ConditionExtract.Controls
{
    public partial class GridViewCondition : Panel
    {
        public GridViewCondition()
        {
            InitializeComponent();
            this.Width = 600;
        }

        public string Headers { get; set; }

        int i1 = 0;

        private struct Item
        {
            public int _right;
            public int _left;
            public object _name;
            public int _y;
        }

        private List<Item> _itemList = new List<Item>();

        public GridViewCondition(IContainer container)
        {
            this.Width = 600;
            
            container.Add(this);

            #region 添加右键菜单
            ContextMenuStrip contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(container);

            ToolStripMenuItem 添加ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            ToolStripMenuItem 删除ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            ToolStripMenuItem 清空ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            // 
            // 添加ToolStripMenuItem
            // 
            添加ToolStripMenuItem.Name = "添加ToolStripMenuItem";
            添加ToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            添加ToolStripMenuItem.Text = "添加";
            添加ToolStripMenuItem.Click += new System.EventHandler(this.添加ToolStripMenuItem_Click);
            // 
            // 删除ToolStripMenuItem
            // 
            删除ToolStripMenuItem.Name = "删除ToolStripMenuItem";
            删除ToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            删除ToolStripMenuItem.Text = "删除";
            删除ToolStripMenuItem.Click += new System.EventHandler(this.删除ToolStripMenuItem_Click);
            // 
            // 清空ToolStripMenuItem
            // 
            清空ToolStripMenuItem.Name = "清空ToolStripMenuItem";
            清空ToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            清空ToolStripMenuItem.Text = "清空";
            清空ToolStripMenuItem.Click += new System.EventHandler(this.清空ToolStripMenuItem_Click);
            contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            添加ToolStripMenuItem,
            删除ToolStripMenuItem,
            清空ToolStripMenuItem});

            contextMenuStrip1.Name = "contextMenuStrip1";
            contextMenuStrip1.Size = new System.Drawing.Size(153, 92);
            this.ContextMenuStrip = contextMenuStrip1;
            #endregion

            //this.comboBox2.Location = new System.Drawing.Point(153, 16);
            //this.comboBox2.Name = "comboBox2";
            //this.comboBox2.Size = new System.Drawing.Size(121, 20);
            //this.comboBox2.TabIndex = 0;
            i1 = 0;
            

        }

        /// <summary>
        /// 添加右键菜单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 添加ToolStripMenuItem_Click(object sender, EventArgs e)
        {
         
            string str = string.Empty;
            if (!string.IsNullOrEmpty(Headers))
            {

                ComboBox combox1 = new ComboBox();
                ComboBox combox2 = new ComboBox();
                ComboBox combox3 = new ComboBox();
                LinkLabel linkLabel1 = new LinkLabel();
                this.AutoSize = true;

                combox1.FormattingEnabled = true;
                combox1.Items.AddRange(new object[] { "等于", "不等于", "包含", "不包含" });
                combox1.Name = "combox1";
                //计算字符串的像素尺寸
                Graphics graph = CreateGraphics();
                SizeF sizeF = graph.MeasureString("不等于", this.Font);
                combox1.Size = new Size((int)(Math.Ceiling(sizeF.Width)) + 20, (int)(Math.Ceiling(sizeF.Height)));


                combox2.FormattingEnabled = true;
                combox2.Items.AddRange(new object[] { "and", "or" });
                combox2.Name = "combox2";
                SizeF sizeF1 = graph.MeasureString("and", this.Font);
                combox2.Size = new Size((int)(Math.Ceiling(sizeF1.Width))+20, (int)(Math.Ceiling(sizeF1.Height)));

                combox3.FormattingEnabled = true;
                combox3.Name = "combox3";

                string[] strs = Headers.Split(new Char[] { ',' },StringSplitOptions.RemoveEmptyEntries);
                int i = strs[0].Length,j=0;
                for(int k=0;k<strs.Length;k++)
                {
                    if (strs[k].Length > i)
                    {
                        i = strs[k].Length;
                        j = k;
                    }
                }
                
                Graphics graph1 = CreateGraphics();
                SizeF sizeF4 = graph1.MeasureString(strs[j].ToString(), this.Font);
                combox3.Size = new Size((int)(Math.Ceiling(sizeF4.Width))+20, (int)(Math.Ceiling(sizeF4.Height)));
                combox3.Items.AddRange(strs);


                Int32 y = 0;
                Item item2 = new Item();
                item2._name = combox3;

                if (_itemList.Count == 0|| _itemList.Count ==1)
                {
                    item2._left = 10;
                    item2._right = combox3.Size.Width+15;
                }
                else
                {
                    if (_itemList.Count % 5 == 1)
                    {
                        item2._left = (Convert.ToInt32(_itemList.Count / 5) + 1)*10;
                    }
                    else
                    {
                        item2._left = 10;
                    }
                    item2._right = item2._left + combox3.Size.Width+15;
                }
                _itemList.Add(item2);
               
                if (_itemList.Count == 1)
                {
                    combox3.Location = new Point(item2._left, 16);
                }
                else
                {
                    combox3.Location = _itemList.Count % 5 == 1 ? new Point(item2._left, _itemList[_itemList.Count-6]._y + 10+combox3.Size.Height) : new Point(item2._left, 16);
                }
                y = combox3.Location.Y;
                item2._y = y;
                _itemList.RemoveAt(_itemList.Count - 1);
                _itemList.Add(item2);
                //item2._left = combox3.Location.X;
                //item2._right = combox3.Location.Y;
                combox3.Tag = i1;
                this.Controls.Add(combox3);

                Item item = new Item();
                item._name = combox1;
                if (_itemList.Count ==1)
                {
                    item._left = _itemList[_itemList.Count - 1]._right + 15;
                    item._right = combox1.Size.Width+15;
                   combox1.Location = new Point(item._left, 16);
                }
                else
                {
                    item._left = _itemList[1]._left;
                    item._right = item._left + combox1.Size.Width+15;
                    combox1.Location = new Point(item._left, y);
                }
                i1++;
                combox1.Tag = i1;
                this.Controls.Add(combox1);
                _itemList.Add(item);

                Item item3 = new Item();
                TextBox tb = new TextBox();
                tb.Width = 150;
                tb.Height = 16;
                item3._name = tb;
                if (_itemList.Count == 2)
                {
                    item3._left = _itemList[_itemList.Count - 1]._left + _itemList[_itemList.Count - 1]._right;
                }
                else
                {
                    item3._left = _itemList[2]._left;
                }
                    item3._right = item3._left + tb.Size.Width + 20;
                    tb.Location =  new Point(item3._left, y);
                i1++;
                tb.Tag = i1;
                this.Controls.Add(tb);
                _itemList.Add(item3);

                Item item4 = new Item();
                item4._name = combox2;
                if (_itemList.Count ==3)
                {
                    item4._left = _itemList[_itemList.Count - 1]._right;
                }
                else
                {
                    item4._left = _itemList[3]._left;
                }
                item4._right = item4._left + combox2.Size.Width + 20;
                combox2.Location = new Point(item4._left, y);
                i1++;
                combox2.Tag = i1;
                this.Controls.Add(combox2);
                _itemList.Add(item4);

                Item item5 = new Item();
                linkLabel1.Name = "linkLabel1";
                linkLabel1.Size = new System.Drawing.Size(29, 12);
                linkLabel1.Text = "删除";
                if (_itemList.Count == 4)
                {
                   // linkLabel1.Tag = 5;
    item5._left = _itemList[_itemList.Count - 1]._right;
                }
                else
                {
                    //linkLabel1.Tag =_itemList.Count+1;
                    item5._left = _itemList[4]._left;
                }
                item5._right = item5._left + linkLabel1.Size.Width +10;
                linkLabel1.Location = new Point(item5._left, y);
                i1++;
                linkLabel1.Tag = i1;
                this.Controls.Add(linkLabel1);
                _itemList.Add(item5);
                linkLabel1.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel1_LinkClicked);
                i1++;
            }
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            int i = 0;
            foreach (var obj in this.Controls)
            {
                i++;
                if (obj.GetType().Name == "LinkLabel")
                {

                    ((LinkLabel)obj).Tag = i;
                }
            }


            int k = Convert.ToInt32(((LinkLabel)sender).Tag);
            int j = k;
            if (k != _itemList.Count)
            {
                if (k < _itemList.Count)
                {
                    k = _itemList.Count - 1;
                    while (_itemList.Count>k)
                    {
                        //把下一行的数据移动到上一行
                        this.Controls[k - 0].Location = this.Controls[k - 5].Location;
                        this.Controls[k - 1].Location = this.Controls[k - 4].Location;
                        this.Controls[k - 2].Location = this.Controls[k - 3].Location;
                        this.Controls[k - 3].Location = this.Controls[k - 2].Location;
                        this.Controls[k - 4].Location = this.Controls[k - 1].Location;
                        k = k + 5;
                    }
                }
            }

            this.Controls.RemoveAt(j - 1);
            this.Controls.RemoveAt(j - 2);
            this.Controls.RemoveAt(j - 3);
            this.Controls.RemoveAt(j - 4);
            this.Controls.RemoveAt(j - 5);

            _itemList.RemoveAt(j - 1);
            _itemList.RemoveAt(j - 2);
            _itemList.RemoveAt(j - 3);
            _itemList.RemoveAt(j - 4);
            _itemList.RemoveAt(j - 5);

        }

        /// <summary>
        /// 刷新页面控件
        /// </summary>
        public void RefushControls()
        {
            //foreach (var obj in this.Controls)
            //{
            //    if (obj.GetType().Name == "ComboBox")
            //    {
            //        if (_itemList.Count == 1)
            //        {
            //            combox3.Location = new Point(item2._left, 16);
            //        }
            //        else
            //        {
            //            combox3.Location = _itemList.Count % 5 == 1 ? new Point(item2._left, _itemList[_itemList.Count - 6]._y + 10 + combox3.Size.Height) : new Point(item2._left, 16);
            //        }

            //       ((ComboBox)obj).Location = new Point(10, 16);
            //    }
            //}

        }


        protected override void OnPaint(PaintEventArgs e)
        {
            this.BorderStyle = BorderStyle.None;
            base.OnPaint(e);
        }

        /// <summary>
        /// 清空右键菜单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 清空ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Controls.Clear();
        }

        /// <summary>
        /// 删除右键菜单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }


    }
}
